package cn.explink.dop.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import cn.explink.dop.entity.vo.DeliveryCompanyVO;
import cn.explink.eap.core.common.dao.impl.GenericBaseCommonDao;

/**
 *
 * @ClassName: DeliveryCompanyDao
 * @Description: 配送公司管理Dao层
 * @author 孟凡岭
 * @date 2015年5月25日 下午6:14:53
 * @since AR1.0
 */
@Repository("deliveryCompanyDao")
public class DeliveryCompanyDao extends GenericBaseCommonDao<DeliveryCompanyVO, String> {
	
	/**
	 * 
	* @Title: queryValiableCompanyId 
	* @Description: 查询可用的公司id集合
	* @author 小强
	* @param @return    设定文件 
	* @return List<String>    返回类型 
	* @throws
	 */
	public List<String> queryValiableCompanyId(){
		List<String> list = new ArrayList<String>();
		StringBuffer sql = new StringBuffer();
		sql.append("select id from bd_deliverycompany com where status=1");
		Query query = this.getSession().createSQLQuery(sql.toString());
		list = query.list();
		return list;
	}
	
	
	/**
	 *
	 * @Title: queryListByPage
	 * @Description: 分页查询
	 * @author 孟凡岭
	 * @param @param company
	 * @param @param pageNum
	 * @param @param pageSize
	 * @param @return
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	public Map<String, Object> queryListByPage(DeliveryCompanyVO company, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer();
		StringBuffer where = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		/**
		 * 查询数据
		 */
		sql.append(" from DeliveryCompanyVO com where 1=1 ");
		if (StringUtils.isNotBlank(company.getCompanyName())) {
			where.append(" and com.companyName like ? ");
			param.add("%" + company.getCompanyName() + "%");
		}
		Query query = this.getSession().createQuery(sql.append(where).toString());
		sql.setLength(0);
		sql.append("select count(*) from DeliveryCompanyVO com where 1=1 ");
		Query countQuery = this.getSession().createQuery(sql.append(where).toString());
		for (int i = 0; i < param.size(); i++) {
			query.setParameter(i, param.get(i));
			countQuery.setParameter(i, param.get(i));
		}
		/**
		 * 设置分页
		 */
		query.setFirstResult((pageNum - 1) * pageSize);
		query.setMaxResults(pageSize);
		Map<String, Object> map = new HashMap<String, Object>();
		/**
		 * 放入list结果集
		 */
		map.put("list", query.list());
		/**
		 * 清空sql，重新拼接，统计数量
		 */
		/**
		 * 获取唯一结果集并放入map
		 */
		int count = Integer.parseInt(countQuery.uniqueResult().toString());
		map.put("count", count);
		return map;
	}

	/**
	 *
	 * @Title: updateEmployeeNum
	 * @Description: 更新配送公司人员数量
	 * @author 孟凡岭
	 * @param @param companyId
	 * @param @param type，类型，1：加，2：减
	 * @param @param num，数量
	 * @return void 返回类型
	 * @throws
	 */
	public void updateEmployeeNum(String companyId, int type, int num) {
		// TODO Auto-generated method stub
		String sql = "update bd_deliverycompany set employee_Num=employee_Num";
		if (type == 1) {
			sql += "+" + num;
		} else {
			sql += "-" + num;
		}
		sql += " where id=?";
		this.getJdbcTemplate().update(sql, companyId);
	}

	/**
	 *
	 * @Title: updateStatus
	 * @Description: 更改配送公司状态
	 * @author 孟凡岭
	 * @param @param ids
	 * @param @param status
	 * @return void 返回类型
	 * @throws
	 */
	public void updateStatus(final String[] ids, int status) {
		// TODO Auto-generated method stub
		String sql = "update bd_deliverycompany set status=" + status + " where id=? ";
		this.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				// TODO Auto-generated method stub
				ps.setString(1, ids[i]);
			}

			@Override
			public int getBatchSize() {
				// TODO Auto-generated method stub
				return ids.length;
			}
		});
	}
}
